package com.shj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shj.mapper.SysSubjectDao;
import com.shj.model.*;
import com.shj.util.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * (SysSubject)表服务实现类
 *
 * @author makejava
 * @since 2020-03-12 12:42:46
 */
@Service("sysSubjectService")
public class SysSubjectService {
    @Resource
    private SysSubjectDao sysSubjectDao;
    @Autowired
    private SysSubFreeService sysSubFreeService;

    /**
     * @Description 根据项目编号查找
     * @author songhuajian
     * @date 2020/6/7 15:31
     */
    public SysSubject findBySubId(String subId) {
        return sysSubjectDao.queryBySubId(subId);
    }


    /**
     * @Description 管理员分页查询数据
     * @author songhuajian
     * @date 2020/3/19 15:00
     */
    public PageResult findPage(PageRequest pageRequest, String leaderId){
        List<SysSubject> list = sysSubjectDao.queryAll(pageRequest.getTitle(), leaderId);
        return PageUtil.getPageResult(pageRequest, getPageInfo(pageRequest, list));
    }

    /**
     * @Description 查询立项通过的项目
     * @author songhuajian
     * @date 2020/6/8 15:40
     */
    public PageResult findForConlusion(PageRequest pageRequest, String leaderId) {
        List<SysSubject> list = sysSubjectDao.findForConclusion(pageRequest.getTitle(), leaderId);
        return PageUtil.getPageResult(pageRequest, getPageInfo(pageRequest, list));
    }

    /**
     * @Description 调用分页插件完成分页
     * @author songhuajian
     * @date 2020/3/19 14:38
     */
    public PageInfo<SysSubject> getPageInfo(PageRequest pageRequest, List<SysSubject> list){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<SysSubject>(list);
    }

    /**
     * 新增数据
     *
     * @param sysSubject 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public SysSubject insert(SysSubject sysSubject) throws IOException {
        //获取sub_id(时间+随机字符）
        String subId = MyIdUtil.getForTimeId();
        Timestamp subTime = MyTimeUtil.getForTimestamp();

        // 添加立项信息
        sysSubject.setSubId(subId);
        sysSubject.setSubTime(subTime.toString());
        sysSubject.setSubStatus(0);

        // 添加预算费用
        String freeJson = (String) sysSubject.getFree();
        SysSubFree sysSubFree = (SysSubFree) JsonUtil.jsonToObj(new SysSubFree(), freeJson);
        sysSubFree.setSubId(subId);
        sysSubFree.setFreeType(0);
        sysSubFreeService.insert(sysSubFree);

        this.sysSubjectDao.insert(sysSubject);
        return sysSubject;
    }

    /**
     * @Description 负责人下载立项申请书
     * @author songhuajian
     * @date 2020/6/6 19:48
     */
    public HWPFDocument downloadFile(String subId) throws IOException {
        // 立项申请书模板
        String url = "G:\\立项申请书.doc";
        File downloadFile = new File(url);
        FileInputStream fileInputStream = new FileInputStream(downloadFile);
        //查找出数据
        SysSubject sysSubject = sysSubjectDao.queryBySubId(subId);
        SysSubFree sysSubFree = sysSubFreeService.queryBySubId(subId, 0);
        //将数据写入模板
        HWPFDocument doc = null;
        Map<String, String> map1 = new HashMap<String, String>();
        Map<String, String> map2 = new HashMap<String, String>();
        try {
            map1 = MapUtil.objectToMap(sysSubject);
            map2 = MapUtil.objectToMap(sysSubFree);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        doc = new HWPFDocument(fileInputStream);
        //获取文档内容区域
        Range range = doc.getRange();
        Set<String> keySet = map1.keySet();
        Set<String> keySet2 = map2.keySet();
        for(String key : keySet) {
            String value = map1.get(key);
            range.replaceText("$" + key, value);
        }
        for(String key : keySet2) {
            String value = map2.get(key);
            range.replaceText("$" + key, value);
        }
        fileInputStream.close();
        return doc;
    }

    /**
     * 修改数据
     *
     * @param sysSubject 实例对象
     */
    public void update(SysSubject sysSubject) {
        this.sysSubjectDao.update(sysSubject);
    }

    /**
     * @Description 修改项目状态为结题中
     * @author songhuajian
     * @date 2020/6/24 23:19
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(String subId) {
        sysSubjectDao.updateStatus(subId);
    }

    /**
     * 通过主键删除数据
     *
     */
    public void deleteBySubId(String subId) {
        this.sysSubjectDao.deleteBySubId(subId);
        // 同时删除资金费用
        sysSubFreeService.deleteBySubId(subId);
    }

    public SubjectCensus countSubject() {
        int count = sysSubjectDao.countAll();
        int adopt = sysSubjectDao.countAdopt();
        int unAdopt = sysSubjectDao.countUnAdopt();

        SubjectCensus subjectCensus = new SubjectCensus();
        subjectCensus.setCount(count);
        subjectCensus.setAdopt(adopt);
        subjectCensus.setUnAdopt(unAdopt);
        return subjectCensus;
    }
}